<?php 
namespace app\home\controller;
use think\Controller;
use think\Db;	
use think\Session;
use think\request;

class Deal extends Controller{
	
	public function  deal(){
		//josn变成数组
		//验签
		$ste=$_POST;
		$str=json_encode($ste);
		$data=json_decode($str,true);
		file_put_contents('./ass01.txt',$data);
/*		$stte='{"respCode":"00","merCode":"330100710271","status":"01","reqSeq":"20180627085801213483","respDesc":"成功","type":"1","version":"1.0.0","amount":"0.10","serialNo":"2018062710153984","orderNo":"20180627597985","chainNo":"004","transCode":"GWP0004"}';
//		file_put_contents('./smk.txt',$str);
//		$ste=iconv('UTF-8','GBK',$ste);
//      $ste=iconv('GBK','UTF-8',$ste);
		//$sign=$data['sign'];
		$data=json_decode($ste,true);
		var_dump($data);
		$sign=$data['sign'];
		unset($data['sign']);
		//$str=json_encode($ste);		
	/*	echo '<br>';
			echo 'data:'.$data;
			echo '<br>';
				echo 'sign:<br>'.$sign;
				echo '<br>';
	    $ste="merCode=330100710271&respDesc=成功&amount=0.10&orderNo=20180627597985&chainNo=004&type=1&reqSeq=20180627085801213483&version=1.0.0&serialNo=2018062710153984&transCode=GWP0004&respCode=00&status=01";
		$ste=iconv('GBK','UTF-8',$ste);
        $res=$this->verify('http://test.lcqjgj.com/static/home/lc.pfx',$stte,'ykGjtBgNO3qukHgKcad0TohFQABCNKPKV1WtuRF+QFTRmi73ntwGKvhFoy8JBHiN+6aD3iMsGvNo5p+lwlusq6bm6JRRS1RXyY6RQEQ\/ZfkmKW7AFb3yFX1m9ftiCfgkF4DzoR2EnOfifsRbS2jOuPduWIEmElXrXov4f0Gubgw=');
        echo 'res:'.$res;die;
        var_dump($res);die;*/
       
        
	    //成功还是失败
		if($data['status']=='01'){
			//支付成功
			//查用户对应充值id
			$arr=Db::name('user_recharge')
			->where('tradeNo',$data['serialNo'])
			->find();
			if($arr['paymentstate']==1){
				return false;
			}
			//充值金额和手续费
			if(!$arr){
				return false;
			}
			
			if($data['version']!='1.0.0'){
				return false;
			}
			
			if($data['chainNo']!='004'){
				return false;
			}
			
			if($data['transCode']!='GWP0004'){
				return false;
			}
			
			//查询手续费率
			$charge=Db::name('payments')->where('id=6')->field('ServiceCharge')->find();
			$a=1+$charge['ServiceCharge'];
			//充值金额
			$amount=$data['amount']/$a*1;
			//手续费
			$charge=$amount*$charge['ServiceCharge'];
			
		    
			//改充值表状态
			$time=date('Y-m-d H:i:s');
			Db::table('qfll_user_recharge')->update(['id'=>$arr['id'],'paymentstate'=>1,'paymenttime'=>$time,'platformtradeNo'=>$data['reqSeq']]);
			//日志表
			 $zprice=Db::name('cashlog')->where('uid',$arr['uid'])->order('id', 'desc')->limit(0,1)->find();
			 $balance=$zprice['balance']+$amount;
			 $ste=['uid'=>$arr['uid'],'type'=>1,'price'=>$amount,'balance'=>$balance];
			 Db::table('qfll_cashlog')->insert($ste);
			 //存入用户余额
			 Db::table('qfll_users')->where('id', $arr['uid'])->setInc('balance', $amount);

			 
		   }else{
		   	//支付失败
		   	$arr=Db::table('qfll_user_recharge')
			->where('retradeNo',$data['serialNo'])
			->where('platformtradeNo',$data['orderNo'])
			->setInc('paymentstate',3);

		  }
		

	}
	
 public function verify($pfxPath,$data, $signature)
    {
        $certs = array();
        openssl_pkcs12_read(file_get_contents($pfxPath), $certs, 'Lczc_164');
        if(!$certs) {
            return;
        }
        $result = openssl_verify($data, base64_decode($signature), $certs['cert']); // openssl_verify验签成功返回1，失��?0，错误返��?-1
        return $result;
    }
    
  	    /**
		 * 验签  验证签名  基于sha1withRSA
		 * @param $data 签名前的原字符串
		 * @param $signature 签名串
		 * @return bool
		 */

 
    
    public function sign($data,$pfxpath,$pfxpwd) {
    $certs = array();
    openssl_pkcs12_read(file_get_contents($pfxpath), $certs, $pfxpwd); // 其中password为你的证书密码
    if (!$certs) {
		return;
	}
    $signature = '';  
    openssl_sign($data, $signature, $certs['pkey']);
    return base64_encode($signature); 
}
	
	
	
	function https_request($url,$data = null)
{
    // php curl 发起get或者post请求
    // curl 初始化
    $curl = curl_init();	// curl 设置
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    // 判断 $data get  or post
    if ( !empty($data) ) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    // 执行
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;

}
}
?>